package com.wechat.controller;

import com.wechat.annotation.GlobalInterceptor;
import com.wechat.entity.po.GroupInfo;
import com.wechat.entity.po.UserInfoBeauty;
import com.wechat.entity.query.GroupInfoQuery;
import com.wechat.entity.query.UserInfoBeautyQuery;
import com.wechat.entity.vo.PaginationResultVO;
import com.wechat.entity.vo.ResponseVO;
import com.wechat.exception.BusinessException;
import com.wechat.service.GroupInfoService;
import com.wechat.service.UserInfoBeautyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

@RestController("adminGroupController")
@RequestMapping("/admin")
public class AdminGroupController extends ABaseController{
    @Resource
    private GroupInfoService groupInfoService;


    /**
     * 加载群组列表
     * @param groupQuery
     * @return
     */
    /*SQL：select g.*,(select nick_name from user_info u where u.user_id = g.group_owner_id) groupOwnerNickName,
    (select count(1) from user_contact u where u.contact_id = g.group_id and u.contact_type = 2) memberCount
    from group_info g
     */
    @GetMapping("/loadGroup")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO loadGroup(GroupInfoQuery groupQuery) {//这里不需要@RequestBody注解，因为参数是URL查询参数，Spring MVC 会将传过来的参数绑定到对象属性上
        groupQuery.setOrderBy("create_time desc");
        groupQuery.setQueryGroupOwnerName(true);
        groupQuery.setQueryMemberCount(true);
        PaginationResultVO resultVO = groupInfoService.findListByPage(groupQuery);
        return getSuccessResponseVO(resultVO);
    }

    /**
     * 解散群组
     * @param groupId
     * @return
     */
    @RequestMapping("/dissolutionGroup")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO dissolutionGroup(@NotNull String groupId) {
        GroupInfo groupInfo = groupInfoService.getGroupInfoByGroupId(groupId);
        if (groupInfo == null) {
            throw new BusinessException("群组不存在");
        }
        String groupOwnerId = groupInfo.getGroupOwnerId();
        groupInfoService.dissolutionGroup(groupOwnerId, groupId);
        return getSuccessResponseVO(null);
    }


}
